package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityTextBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.IntentUtil;
import stark.common.bean.StkResBean;

public class TextActivity extends BaseAc<ActivityTextBinding> {

    public static StkResBean stkResBean;
    private boolean isCollect;
    private List<StkResBean> listSp;

    @Override
    protected int onCreate() {
        return R.layout.activity_text;
    }

    @Override
    protected void initView() {
        mDataBinding.ivTextBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivShare.setOnClickListener(this);
        mDataBinding.ivCopy.setOnClickListener(this);
        mDataBinding.ivCollect.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivShare://分享
                IntentUtil.shareText(mContext, stkResBean.getDesc());
                break;
            case R.id.ivCopy://复制
                ClipboardUtils.copyText(stkResBean.getDesc());
                ToastUtils.showShort(getString(R.string.copy_success_tips));
                break;
            case R.id.ivCollect://收藏
                if (isCollect) {
                    listSp.remove(stkResBean);
                    AppSpUtils.saveCollectList(listSp);
                    isCollect = false;
                    mDataBinding.ivCollect.setImageResource(R.drawable.asc1);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(stkResBean);
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveCollectList(listSp);
                    } else {
                        AppSpUtils.saveCollectList(listSave);
                    }
                    isCollect = true;
                    mDataBinding.ivCollect.setImageResource(R.drawable.asc3);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvTextContent.setText(stkResBean.getDesc());
        setCollectData();
    }

    //收藏
    private void setCollectData() {
        listSp = AppSpUtils.getCollectList();
        isCollect = false;
        mDataBinding.ivCollect.setImageResource(R.drawable.asc1);
        if (listSp != null && listSp.size() != 0) {
            for (StkResBean bean : listSp) {
                if (bean.equals(stkResBean)) {
                    isCollect = true;
                    mDataBinding.ivCollect.setImageResource(R.drawable.asc3);
                    break;
                }
            }
        }
    }
}